<?php
/**
* Copyright 2009 Waag Society <http://www.waag.org>
* This file is part of Annotrax.
*
* Annotrax is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* Annotrax is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Annotrax.
* If not, see http://www.gnu.org/licenses/gpl.html
*/

?>
<?php
/**
 * Model class for Annotrax admin users
 *
 * @author Matthijs van Henten  <Matthijs@waag.org>
 * @license GNU GPL 2.0
 * @verson 0.1
 * @copyright Copyright (c) 2009, Waag Society
 */

class Default_Model_AdminUser{
    private $id;
    private $username;
    private $password;
    private $email;
    private $name;

    protected $mapper;

    public function __get( $name ){
        //echo "GETTING $name";
        if( property_exists( $this, $name ) ){
            if( null === $this->$name ){
                $this->find();
            }
            return $this->$name;
        }
    }

    public function __set( $name, $value ){
        if( $method = 'set' . ucfirst( $name ) && method_exists( $this, $name ) ){
            call_user_func_array( array( $this, $method ), $value );
        }
        else if( property_exists( $this, $name ) ){
            $this->$name = $value;
        }
    }

	/**
	 * Set (new) password for this user.
	 *
	 * @param string $password Plain text-string for the password
	 * @return $this Fluent interface
	 */
	public function setPassword( $password ){
		$this->password = md5( $password );
		return $this;
	}

	public function generatePassword(){
		$str = '';
		$ranges = array(
			str_split('!@#$%^&*_-+'),
			range(0, 10),
			range('a', 'z'),
			range('A', 'Z')
		);

		$range = array();

		foreach( $ranges as $part ){
			$range = array_merge( $range, $part);
		}

		foreach( range(0, rand(6,10) ) as $index ){
			shuffle( $range );
			$str .= array_shift( $range );
		}
	}

    /**
     * Save the user
     *
     * @return Default_Model_User $user
     */
    public function save(){
        $this->getMapper()
            ->save( $this );

        return $this;
    }

    /**
     * Fetch or instantiates the mapper
     *
     * @return object default_model_mapper
     */
    private function getMapper(){
        if( $this->mapper === null ){
            $mapper = new Default_Model_AdminUserMapper();
            $this->setMapper( $mapper    );
        }
        return $this->mapper;
    }

    /**
     * Set mapper
     *
     * @param default_model_mapper $mapper
     */
    private function setMapper( $mapper ){
        $this->mapper = $mapper;
        return $this;
    }

    /**
     * Retrieve session namespace
     */
    private function getSession(){
        if( null === $this->session ){
            $this->session = new Zend_Session_Namespace( 'user_admin' );
        }
        return $this->session;
    }
}
